/*
 * ==========================================================================*\
 * | $Id$
 * |*-------------------------------------------------------------------------*|
 * | Copyright (C) 2009 Virginia Tech | | This file is part of CloudSpace. | |
 * CloudSpace is free software; you can redistribute it and/or modify | it under
 * the terms of the GNU General Public License as published | by the Free
 * Software Foundation; either version 3 of the License, or | (at your option)
 * any later version. | | CloudSpace is distributed in the hope that it will be
 * useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of |
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General
 * Public License for more details. | | You should have received a copy of the
 * GNU General Public License | along with CloudSpace; if not, see
 * <http://www.gnu.org/licenses/>.
 * \*==========================================================================
 */

package cloudspace.ui;

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Textbox;

import cloudspace.ui.impl.PasswordDialog;
import cloudspace.ui.impl.PromptDialog;
import cloudspace.ui.impl.TextEditorDialog;
import cloudspace.vm.filesystem.CSPath;


// -------------------------------------------------------------------------
/**
 * A class containing utility methods for popping up useful modal dialogs.
 * 
 * @author Tony Allevato
 * @author Last changed by $Author$
 * @version $Revision$, $Date$
 */
public class Dialogs
{
    // ~ Constructors ..........................................................

    // ----------------------------------------------------------
    /**
     * Static class; prevent instantiation.
     */
    private Dialogs()
    {
        // Static class; prevent instantiation.
    }


    // ~ Methods ...............................................................

    // ----------------------------------------------------------
    /**
     * Displays a prompt dialog that asks the user to enter a string.
     * 
     * @param title
     *            the title of the dialog
     * @param prompt
     *            the short prompt to display to the left of the input field
     * @param initialValue
     *            the initial value of the input field; may be null
     */
    public static String prompt(
        String title,
        String prompt,
        String initialValue )
    {
        HashMap<String, Object> args = new HashMap<String, Object>();

        args.put( "title", title );
        args.put( "prompt", prompt );
        args.put( "initialValue", initialValue );

        PromptDialog dialog = (PromptDialog)Executions.createComponents( "/partials/dialogs/prompt.zul",
            null,
            args );

        Textbox promptValue = (Textbox)dialog.getFellow( "promptValue" );
        if ( initialValue != null )
        {
            promptValue.setSelectionRange( 0, initialValue.length() );
        }

        promptValue.focus();

        try
        {
            dialog.doModal();
        }
        catch ( Exception e )
        {
            log.error( "An exception occurred when displaying the modal "
                + "prompt dialog", e );
            dialog.detach();
        }

        return dialog.getResult();
    }


    // ----------------------------------------------------------
    /**
     * Displays a dialog that can be used to edit a large body of text.
     * 
     * @param title
     *            the title of the dialog
     * @param initialContent
     *            the initial content of the text editor; may be null
     * @param targetFile
     */
    public static String textEditor(
        String title,
        String initialContent,
        CSPath targetFile )
    {
        HashMap<String, Object> args = new HashMap<String, Object>();

        args.put( "title", title );
        args.put( "initialContent", initialContent );

        TextEditorDialog dialog = (TextEditorDialog)Executions.createComponents( "/partials/dialogs/textEditor.zul",
            null,
            args );
        dialog.setTarget( targetFile );
        Textbox textArea = (Textbox)dialog.getFellow( "textArea" );
        textArea.focus();
        textArea.setMultiline( true );

        try
        {
            dialog.doModal();
        }
        catch ( Exception e )
        {
            log.error( "An exception occurred when displaying the modal "
                + "text editor dialog", e );
            dialog.detach();
        }

        return dialog.getContent();
    }

    // ~ Static/instance variables .............................................

    private static final Logger log = Logger.getLogger( Dialogs.class );


    public static String passwordResetPrompt()
    {
        PasswordDialog dialog = (PasswordDialog)Executions.createComponents( "/partials/dialogs/passwordReset.zul",
            null,
            null );

        Textbox promptValue = (Textbox)dialog.getFellow( "promptValue1" );
        promptValue.focus();

        try
        {
            dialog.doModal();
        }
        catch ( Exception e )
        {
            log.error( "An exception occurred when displaying the modal "
                + "prompt dialog", e );
            dialog.detach();
        }

        return dialog.getNewPassword();
    }
}
